/*
 * ==========================================================================*\
 * | $Id$
 * |*-------------------------------------------------------------------------*|
 * | Copyright (C) 2009 Virginia Tech | | This file is part of CloudSpace. | |
 * CloudSpace is free software; you can redistribute it and/or modify | it under
 * the terms of the GNU General Public License as published | by the Free
 * Software Foundation; either version 3 of the License, or | (at your option)
 * any later version. | | CloudSpace is distributed in the hope that it will be
 * useful, | but WITHOUT ANY WARRANTY; without even the implied warranty of |
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | GNU General
 * Public License for more details. | | You should have received a copy of the
 * GNU General Public License | along with CloudSpace; if not, see
 * <http://www.gnu.org/licenses/>.
 * \*==========================================================================
 */

package cloudspace.ui.impl;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import cloudspace.vm.filesystem.CSPath;
import cloudspace.vm.filesystem.CSPathWriter;


// -------------------------------------------------------------------------
/**
 * The internal implementation of the text editor dialog. Do not use this class
 * directly; call {@link cloudspace.ui.Dialogs#textEditor(String, String)} to
 * display the dialog.
 * 
 * @author Tony Allevato
 * @author Last changed by $Author$
 * @version $Revision$, $Date$
 */
public class TextEditorDialog extends Window
{

    // ~ Static/instance variables .............................................
    private static Logger log = Logger.getLogger( TextEditorDialog.class );

    private static final long serialVersionUID = -6884372948553134675L;

    private String content;

    private CSPath path;


    // ~ Constructors ..........................................................

    // ----------------------------------------------------------
    public TextEditorDialog()
    {
        content = null;
    }


    // ~ Methods ...............................................................

    public void onSave()
    {
        Textbox textbox = (Textbox)getFellow( "textArea" );
        content = textbox.getValue();
        if ( content != null )
        {
            CSPathWriter writer = null;

            try
            {
                writer = new CSPathWriter( path );
                IOUtils.write( content, writer );
            }
            catch ( IOException e )
            {
                try
                {
                    Messagebox.show( "Could not write to the File",
                        "Error",
                        Messagebox.OK,
                        Messagebox.ERROR );
                }
                catch ( InterruptedException e1 )
                {
                    log.error( "An error occurred when displaying the error "
                        + "dialog", e );
                }
            }
            finally
            {
                if ( writer != null )
                {
                    try
                    {
                        writer.close();
                    }
                    catch ( IOException e )
                    {
                        // Do nothing.
                    }
                }
            }
        }
    }


    // ----------------------------------------------------------
    public void onOK()
    {
        Textbox textbox = (Textbox)getFellow( "textArea" );
        endModal( textbox.getValue() );
    }


    // ----------------------------------------------------------
    public void endModal( String content )
    {
        this.content = content;
        detach();
    }


    // ----------------------------------------------------------
    public String getContent()
    {
        return content;
    }


    public void setTarget( CSPath targetFile )
    {
        path = targetFile;
    }
}
